package com.smartboot.plus.service;

import com.smartboot.plus.ignite.IgniteCacheRegion;
import com.smartboot.plus.ignite.listener.DisconnectMessage;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteMessaging;
import org.apache.ignite.cluster.ClusterGroup;
import tech.smartboot.feat.cloud.annotation.Autowired;
import tech.smartboot.feat.cloud.annotation.Bean;
import tech.smartboot.feat.cloud.annotation.PostConstruct;

@Bean
public class MqttMessageService {

    @Autowired
    private Ignite igniteInstance;


    private ClusterGroup cacheGroup;

    public ClusterGroup getCacheGroup() {
        return cacheGroup;
    }

    @PostConstruct
    public void init() {
        // ...
        IgniteMessaging rmtMsg = igniteInstance.message(igniteInstance.cluster().forRemotes());
        cacheGroup = rmtMsg.clusterGroup().forCacheNodes(IgniteCacheRegion.MQTT_AUTH_CACHE.getCacheName());
        //方法在集群内的所有节点上注册了一个带有指定主题的监听器然后监听来自集群内任意节点的消息。
        cacheGroup.ignite().message().remoteListen("disconnectTopic", new DisconnectMessage());
    }
}
